# This file is part of the Net::Tube module.
#
# Net::Tube is Free Software; you can use, redistribute it and/or
# modify it under the terms of either:
#
# a) the GNU General Public License as published by the Free Software
#    Foundation, either version 2, or (at your opinion) any later
#    version, or
#
# b) the "Artistic License" which comes with Perl.
#
use strict;
use warnings;
use Test::Simple tests => 4;

use Net::Tube;
use Net::Tube::Loader::Net;

sub test_new {
    my $x = Net::Tube::Loader::Net->new();
    unless ( defined $x && ref $x ) {
        print "#object not created \n";
        return 0;
    }
    unless ( $x->isa("Net::Tube::Loader::Net") ) {
        print "# wrong object type created\n";
        return 0;
    }
    return 1;
}

ok( test_new(), "test new" );

sub test_new2 {
    my $x = Net::Tube::Loader::Net->new({loadSource =>[qw(http://lo-f.at http://www.ou.nl)]});
    unless ( defined $x && ref $x ) {
        print "#object not created \n";
        return 0;
    }
    unless ( $x->isa("Net::Tube::Loader::Net") ) {
        print "# wrong object type created\n";
        return 0;
    }
    
    my @sources = $x->get_config_entry('loadSource');
    unless (scalar @sources == 2) {
        print "# strange configuration found \n ";
        return 0; 
    }
    
    unless ($sources[0] eq "http://lo-f.at") {
        print "# wrong url found at first position\n";
        return 0;
    }
    
    unless ($sources[1] eq "http://www.ou.nl") {
        print "# wrong url found at second position\n";
        return 0;
    }
    return 1;
}

ok( test_new2(), "test new with parameters" );

sub test_get_simple {
    my $x = Net::Tube::Loader::Net->new({loadSource =>[qw(http://lo-f.at/glahn/blog/index.xml)]});
    unless ( defined $x && ref $x ) {
        print "#object not created \n";
        return 0;
    }
    
    my $b = $x->process(undef);
    unless ( defined $b ) {
        print "# no bucket returned!\n";
        return 0;
    }
    
    unless ($b->count() == 1 ) {
        print "# bad bucket count: ". $b->count() ."\n";
        return 0;
    }
   
    my $n = $b->get_node(0);
    unless ( defined $n && ref $n ) {
        print "# no node found although count was OK\n";
        return 0;
    }
    
    unless ($n->documentElement->nodeName eq "document") {
        print "# wrong type of document found\n";
        return 0;
    }
    
    my @nds = $n->findnodes('//entry');
    unless (scalar(@nds) == 15) {
        print "# wrong number of elements found!\n";
        return 0;
    }
    
    return 1;
}
 
ok( test_get_simple(), "test simple get" );
 
sub test_cached_get {
    # warn $0 . "\n";
    # warn $path . "\n";
    
    my $turl = 'http://lo-f.at/glahn/blog/index.xml';
    my $x = Net::Tube::Loader::Net->new({loadSource =>[$turl]});
    unless ( defined $x && ref $x ) {
        print "#object not created \n";
        return 0;
    }
    my $m = Net::Tube::Machine->new();
    $m->cache_defaults({cache_root => 'tcache'});
    my $data;
    
    eval { $data = $m->execute( [ $x ] ); };
    if ($@) {
        print "# internal error: $@\n";
        return 0;
    }
    unless ( defined $data && length $data ) {
        print "# request failed for some strange reason\n";
        return 0;
    } 
    
    my $co = $m->cache();
    unless ( defined $co ) {
        print "# lost the cache object!\n";
        return;
    }
    
    my $tdta = $co->get($turl);
    unless ( defined $tdta && length $tdta ){
        print "# no data found in cache\n";
        return 0;
    }
    unless ( $tdta eq $data ) {
        print "# different data in cache than received from the server\n";
        return 0;
    }
    
    eval { $data = $m->execute( [ $x ] ); };
    if ($@) {
        print "# internal error: $@\n";
        return 0;
    }
     
    return 1;
}
ok( test_cached_get(), "test cached get");
